package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITagService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService catalogService;
    @Autowired
    private IStrategyCommentService commentService;
    @Autowired
    private ITagService tagService;


    @GetMapping
    public PageInfo list(StrategyQueryObject qo) {
        qo.setPageSize(0);
        return strategyService.query(qo);
    }

    /**
     * 根据攻略的id查询攻略信息数据
     *
     * @param strategyId
     * @return
     */
    @GetMapping("{strategyId}")
    public Strategy getStrategyById(@PathVariable("strategyId") Long strategyId) {
        return strategyService.selectStrategyById(strategyId);
    }

    /**
     * 根据某个攻略查询这个攻略下的所有目录
     *
     * @param strategyId
     * @return
     */
    @GetMapping("{strategyId}/catalogs")
    public List<StrategyCatalog> getCatalogsByStrategyId(@PathVariable Long strategyId) {
        return catalogService.selectByStrategyId(strategyId);
    }

    @GetMapping("{strategyId}/comments")
    public PageInfo selectCommentByStrategyId(StrategyCommentQueryObject qo) {
        qo.setOrderBy("sc.createTime desc");
        PageInfo pageInfo = commentService.query(qo);
        return pageInfo;
    }

    @GetMapping("{strategyId}/tags")
    public List<Tag> getTagsByStrategyId(@PathVariable Long strategyId) {
        return tagService.getTagByStrategyId(strategyId);
    }

    @PostMapping("{strategy.id}/comments")
    public JSONResult save(StrategyComment strategyComment, String[] tags) {
        commentService.save(strategyComment, tags);
        return new JSONResult();
    }

    /**
     * 根据点评Id获取点评的内容和点评作者的信息
     *
     * @param commentId
     * @return
     */
    @GetMapping("{commentId}/comment")
    public StrategyComment getCommentById(@PathVariable Long commentId) {
        return commentService.getCommentById(commentId);
    }

    /**
     * 根据状态查询所有的点评
     * @param qo
     * @return
     */
    @GetMapping("/comments")
    public List<StrategyComment> allComments(StrategyCommentQueryObject qo) {
        List<StrategyComment> list =  commentService.allComments(qo);
        return list;
    }

    /**
     * 根据作者id查询所有的点评
     * @param qo
     * @return
     */
    @GetMapping("/comments/{authorId}")
    public PageInfo selectCommentsByAuthor(StrategyCommentQueryObject qo) {
        qo.setOrderBy("sc.createTime desc");
        PageInfo pageInfo = commentService.selectCommentsByAuthor(qo);
        return pageInfo;
    }



}
